package cn.jsu.projectmanage.service.imp;

import cn.jsu.projectmanage.dao.projectManage.GroupDao;
import cn.jsu.projectmanage.dao.projectManage.TaskDao;
import cn.jsu.projectmanage.pojo.dto.Group;
import cn.jsu.projectmanage.pojo.dto.GroupUser;
import cn.jsu.projectmanage.pojo.dto.Project;
import cn.jsu.projectmanage.pojo.vo.CommonResult;
import cn.jsu.projectmanage.pojo.vo.GroupVo;
import cn.jsu.projectmanage.pojo.vo.ProjectVo;
import cn.jsu.projectmanage.pojo.vo.QueryProjectTaskVo;
import cn.jsu.projectmanage.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class GroupServiceImp implements GroupService {

    @Autowired
    GroupDao groupDao;

    @Autowired
    TaskDao taskDao;

    /**
     * 先创建小组并将组员加入关联表，再在项目表中插入项目信息
     * @param project
     * @return
     */
    @Override
    @Transactional
    public CommonResult createProject(ProjectVo project) {
        //0为管理员Id
        Group group = new Group(0, null, project.getProject().getProjectName(), project.getProjectLeadId());
        groupDao.insertGroup(group);
        int groupId = group.getGroupId();
        List<GroupUser> groupUsers = new ArrayList<>();
        for (Integer userId: project.getUserIds()) {
            groupUsers.add(new GroupUser(0, groupId, userId));
        }
        groupDao.insertGroupUsers(groupUsers);
        project.getProject().setProjectStatus(Project.ProjectStatus.START.getValue());
        project.getProject().setProjectGroup(groupId);
        groupDao.insertProject(project.getProject());
        return new CommonResult<>(200, "项目创建成功", project.getProject().getProjectId());
    }

    @Override
    public CommonResult queryProjectAll() {
        List<Project> projects = groupDao.queryProjectAll();
        return new CommonResult<>(200, "查询成功", projects);
    }


    @Override
    public CommonResult queryProjectById(int projectId) {
        Project project = groupDao.queryProjectById(projectId);
        return new CommonResult<>(200, "查询成功", project);
    }

    /**
     * 查询项目中符合查询类型和状态的任务
     * @param queryProjectTask
     * @return
     *
     */
    @Override
    public CommonResult queryProjectTaskNumber(QueryProjectTaskVo queryProjectTask) {
        //查询项目中叶任务的总数
        Integer taskNumber = taskDao.queryProjectTaskNumber(queryProjectTask.getProjectId()
        , queryProjectTask.getStatus(), queryProjectTask.getTaskType());
        return new CommonResult(200,"查询数量成功",taskNumber);

    }

    @Override
    @Transactional
    public CommonResult createGroup(GroupVo group) {
        groupDao.insertGroup(group.getGroup());
        Integer groupId = group.getGroup().getGroupId();
        List<GroupUser> groupUsers = new ArrayList<>();
        for (Integer userId: group.getGroupUsers()) {
            groupUsers.add(new GroupUser(0, groupId, userId));
        }
        groupDao.insertGroupUsers(groupUsers);
        return new CommonResult<>(200, "小组创建成功", groupId);
    }

    @Override
    public CommonResult addGroupUsers(int groupId, List<Integer> userIds) {
        List<GroupUser> groupUsers = new ArrayList<>();
        for (Integer userId: userIds) {
            groupUsers.add(new GroupUser(0, groupId, userId));
        }
        groupDao.insertGroupUsers(groupUsers);
        return new CommonResult<>(200, "添加用户成功", null);
    }

    @Override
    public CommonResult deleteGroupUsers(Integer groupId, List<Integer> userIds) {
        groupDao.deleteGroupUsers(groupId, userIds);
        return new CommonResult(200, "删除成功");
    }

    @Override
    public CommonResult updateGroupName(int groupId, String groupName) {
        try {
            groupDao.updateGroupName(groupId,groupName);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(444, "修改失败");
        }

        return new CommonResult<>(200, "修改成功");
    }


    @Override
    public CommonResult queryGroupById(int groupId) {
        Group group = groupDao.queryGroupById(groupId);
        return new CommonResult<>(200, "查询成功", group);
    }

    @Override
    public CommonResult queryGroupByGroup(int groupId) {
        List<Group> groups = groupDao.queryGroupByGroup(groupId);
        return new CommonResult<>(200, "查询成功", groups);
    }

}
